package io.externalize;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Scanner;

public class StudentDatabase {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {

		Scanner sc = new Scanner(System.in);
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"object.dat", true));
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"object.dat"));
		String ch = "n";
		do {
			Student obj = new Student();
			obj.create();
			oos.writeObject(obj);
			System.out.print("Add Another student(y/n): ");
			ch = sc.next();
		} while (ch.charAt(0) == 'y' || ch.charAt(0) == 'Y');
		oos.close();
		System.out.println("RollNo\tName\tAge\tAddress");
		try {
			do {
				Object myObj = ois.readObject();
				Student s = (Student) myObj;
				System.out.println(s);
			} while (true);
		} catch (EOFException e) {
			System.out.println("EndOfFile reached.");
		} catch (StreamCorruptedException e) {
			System.out.println("File Corrupted.");
		}
		ois.close();
	}
}
